package com.git.bcs.common.bean.adapter;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.annotation.JsonInclude;

@ControllerAdvice
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter  {

	  @Override
	    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
	        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
	        ObjectMapper objectMapper = builder.build();
	        SimpleModule simpleModule = new SimpleModule();
	        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
	        objectMapper.registerModule(simpleModule);
	        objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);// 忽略 transient 修饰的属性
	        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
	        super.configureMessageConverters(converters);
	    }
}
